;
!(function() {
    //购物车数据渲染
    //window.location.search.slice(5)
    $.ajax({
        url: 'http://10.31.158.42/javascript/projectname/php/goodsdata.php',
        dataType: 'json',
        type: 'post',

    }).done(function(d) {
        const $buy = $('.bot .buy');
        const $shul = $('.center .shuliang');
        let arrsid = [];
        let arrnum = [];

        $buy.on('click', function() {
            if (getcookie('name') && $shul != '') {
                alert('加入购物车成功');
                if (getcookie('cookiesid') && getcookie('cookienum')) {
                    arrsid = getcookie('cookiesid').split(',');
                    arrnum = getcookie('cookienum').split(',');
                }

                let id = window.location.search.slice(5);

                if (arrsid.indexOf(id) == -1) {
                    arrsid.push(id);
                    arrnum.push($shul.val());
                    setcookie('cookiesid', arrsid, 7);
                    setcookie('cookienum', arrnum, 7);
                } else if (arrsid.indexOf(id)) {
                    let sum = 0;
                    sum = Number(arrnum[arrsid.indexOf(id)]) + Number($shul.val());
                    arrnum[arrsid.indexOf(id)] = sum;
                    setcookie('cookienum', arrnum, 7);
                }
            } else if ($shul == '') {
                alert('请填写购买数量');
            } else {
                alert('先登录，后购物');
            }
        });

        let strhtml1 = '';
        let strhtml2 = '';
        let strhtml3 = '';
        let $smallpic = $('.smallbox .smallpic');
        let $bigbox = $('.bigbox');
        let $img = $('.picSlide');
        let $num = $('.center .num');
        let $price = $('.top .price');
        let $title = $('.top .title');
        let i = window.location.search.slice(5) - 1;
        $smallpic.find('img').attr('src', `${d[i].img}`);
        $bigbox.find('img').attr('src', `${d[i].img}`);
        $strhtml3 = `${d[i].title}`;
        strhtml2 = `<span>￥${d[i].price}</span>`;
        let arr = [];
        arr = d[i].imgs.split(',')
        $.each(arr, function(index, value) {
            strhtml1 += `<li><img src="${value}"></li>`;
        });
        $title.html($strhtml3);
        $img.html(strhtml1);
        $('title').html($strhtml3);
        $num.html(strhtml2);
        $price.html(strhtml2);

        $oLi = $('.picSlide li');
        $bigpic = $('.bigpic');
        $oLi.on('click', 'img', function() { //事件委托
            let $imgurl = $(this).attr('src');
            $smallpic.find('img').attr('src', $imgurl);
            $bigpic.attr('src', $imgurl);
        });

    });
})(jQuery);



!(function() {
    //放大镜
    $('.smallbox').on('mousemove', function(ev) {
        $('.smallbox .sf').show();
        $('.bigbox').show();
        var ev = ev || window.event;
        let sortx = ev.pageX;
        let sorty = ev.pageY;

        $('.smallbox .sf').width($('.bigbox').width() * $('.smallbox').width() / $('.bigbox .bigpic').width());
        $('.smallbox .sf').height($('.bigbox').height() * $('.smallbox').height() / $('.bigbox .bigpic').height());

        let bili = $('.bigbox').width() / $('.smallbox .sf').width();

        let width = $('.smallbox .sf').width();
        let width1 = $('.smallbox').width();
        let height = $('.smallbox .sf').height();
        let height1 = $('.smallbox').height();
        let l = sortx - $('.main .left').offset().left - width / 2;
        let t = sorty - $('.main .left').offset().top - height / 2;

        if (l < 0) {
            l = 9;
        } else if (l >= width1 - width) {
            l = width1 - width - 9;
        };

        if (t < 0) {
            t = 9;
        } else if (t >= height1 - height) {
            t = height1 - height - 9;
        }

        $('.smallbox .sf').css({
            left: l,
            top: t,
        });

        $('.bigpic').css({
            left: -l * bili,
            top: -t * bili
        });
    }).on('mouseout', function() {
        $('.smallbox .sf').hide();
        $('.bigbox').hide();

    })
})(jQuery);


!(function() {
    const $imgtop = $('.slid-top');
    const $imgbot = $('.slid-bot');
    $imgtop.mouseleave(function() {
        $('#slid-bigtop').hide();
    });
    $imgtop.mouseenter(function() {
        $('#slid-bigtop').show();
    });
    $imgbot.mouseleave(function() {
        $('#slid-bigbot').hide();
    });
    $imgbot.mouseenter(function() {
        $('#slid-bigbot').show();
    });
})(jQuery);